package sharetime.web.view.flexjson;

import java.util.Map;
import java.util.HashMap;

/**
 * A helper class provided out of the box to encode characters that HTML can't support
 * natively like &lt;, &gt;, &amp;, or &quot;.  This will scan the value passed to the transform
 * method and replace any of these special characters with the HTML encoded equivalent.  This
 * method will NOT work for HTML text because it will blindly encode all characters it sees which
 * means it will strip out any HTML tags.
 */
public class HtmlEncoder implements ITransformer {

    private static final Map<Integer,String> htmlEntities = new HashMap<Integer,String>();

    public HtmlEncoder() {
        if( htmlEntities.isEmpty() ) {
            htmlEntities.put( 34, "&quot;" );       // " - double-quote
            htmlEntities.put( 38, "&amp;" );        // & - ampersand
//            htmlEntities.put( 39, "&apos;");        // ' - apostrophe
            htmlEntities.put( 60, "&lt;" );         // < - less-than
            htmlEntities.put( 62, "&gt;" );         // > - greater-than
            htmlEntities.put( 160, "&nbsp;" );      // non-breaking space
            htmlEntities.put( 169, "&copy;" );      // ï¿½ - copyright
            htmlEntities.put( 174, "&reg;" );       // ï¿½ - registered trademark
            htmlEntities.put( 192, "&Agrave;" );    // ï¿½ - uppercase A, grave accent
            htmlEntities.put( 193, "&Aacute;" );    // ï¿½ - uppercase A, acute accent
            htmlEntities.put( 194, "&Acirc;");      // ï¿½ - uppercase A, circumflex accent
            htmlEntities.put( 195, "&Atilde;" );    // ï¿½ - uppercase A, tilde
            htmlEntities.put( 196, "&Auml;" );      // ï¿½ - uppercase A, umlaut
            htmlEntities.put( 197, "&Aring;" );     // ï¿½ - uppercase A, ring
            htmlEntities.put( 198, "&AElig;" );     // ï¿½ - uppercase AE
            htmlEntities.put( 199, "&Ccedil;" );    // ï¿½ - uppercase C, cedilla
            htmlEntities.put( 200, "&Egrave;");     // ï¿½ - uppercase E, grave accent
            htmlEntities.put( 201, "&Eacute;");     // ï¿½ - uppercase E, acute accent
            htmlEntities.put( 202, "&Ecirc;" );     // ï¿½ - uppercase E, circumflex accent
            htmlEntities.put( 203, "&Euml;" );      // ï¿½ - uppercase E, umlaut
            htmlEntities.put( 204, "&Igrave;" );    // ï¿½ - uppercase I, grave accent
            htmlEntities.put( 205, "&Iacute;" );    // ï¿½ - uppercase I, acute accent
            htmlEntities.put( 206, "&Icirc;" );     // ï¿½ - uppercase I, circumflex accent
            htmlEntities.put( 207, "&Iuml;" );      // ï¿½ - uppercase I, umlaut
            htmlEntities.put( 208, "&ETH;" );       // ï¿½ - uppercase Eth, Icelandic
            htmlEntities.put( 209, "&Ntilde;");     // ï¿½ - uppercase N, tilde
            htmlEntities.put( 210, "&Ograve;");     // ï¿½ - uppercase O, grave accent
            htmlEntities.put( 211, "&Oacute;");     // ï¿½ - uppercase O, acute accent
            htmlEntities.put( 212, "&Ocirc;" );     // ï¿½ - uppercase O, circumflex accent
            htmlEntities.put( 213, "&Otilde;");     // ï¿½ - uppercase O, tilde
            htmlEntities.put( 214, "&Ouml;");       // ï¿½ - uppercase O, umlaut
            htmlEntities.put( 216, "&Oslash;");     // ï¿½ - uppercase O, slash
            htmlEntities.put( 217, "&Ugrave;");     // ï¿½ - uppercase U, grave accent
            htmlEntities.put( 218, "&Uacute;");     // ï¿½ - uppercase U, acute accent
            htmlEntities.put( 219, "&Ucirc;" );     // ï¿½ - uppercase U, circumflex accent
            htmlEntities.put( 220, "&Uuml;" );      // ï¿½ - uppercase U, umlaut
            htmlEntities.put( 221, "&Yacute;");     // ï¿½ - uppercase Y, acute accent
            htmlEntities.put( 222, "&THORN;");      // ï¿½ - uppercase THORN, Icelandic
            htmlEntities.put( 223, "&szlig;");      // ï¿½ - lowercase sharps, German
            htmlEntities.put( 224, "&agrave;");     // ï¿½ - lowercase a, grave accent
            htmlEntities.put( 225, "&aacute;");     // ï¿½ - lowercase a, acute accent
            htmlEntities.put( 226, "&acirc;");      // ï¿½ - lowercase a, circumflex accent
            htmlEntities.put( 227, "&atilde;");     // ï¿½ - lowercase a, tilde
            htmlEntities.put( 228, "&auml;");       // ï¿½ - lowercase a, umlaut
            htmlEntities.put( 229, "&aring;");      // ï¿½ - lowercase a, ring
            htmlEntities.put( 230, "&aelig;");      // ï¿½ - lowercase ae
            htmlEntities.put( 231, "&ccedil;" );    // ï¿½ - lowercase c, cedilla
            htmlEntities.put( 232, "&egrave;");     // ï¿½ - lowercase e, grave accent
            htmlEntities.put( 233, "&eacute;");     // ï¿½ - lowercase e, acute accent
            htmlEntities.put( 234, "&ecirc;");      // ï¿½ - lowercase e, circumflex accent
            htmlEntities.put( 235, "&euml;");       // ï¿½ - lowercase e, umlaut
            htmlEntities.put( 236, "&igrave;" );    // ï¿½ - lowercase i, grave accent
            htmlEntities.put( 237, "&iacute;");     // ï¿½ - lowercase i, acute accent
            htmlEntities.put( 238, "&icirc;");      // ï¿½ - lowercase i, circumflex accent
            htmlEntities.put( 239, "&iuml;");       // ï¿½ - lowercase i, umlaut
            htmlEntities.put( 240, "&eth;");        // ï¿½ - lowercase eth, Icelandic
            htmlEntities.put( 241, "&ntilde;");     // ï¿½ - lowercase n, tilde
            htmlEntities.put( 242, "&ograve;");     // ï¿½ - lowercase o, grave accent
            htmlEntities.put( 243, "&oacute;");     // ï¿½ - lowercase o, acute accent
            htmlEntities.put( 244, "&ocirc;");      // ï¿½ - lowercase o, circumflex accent
            htmlEntities.put( 245, "&otilde;");     // ï¿½ - lowercase o, tilde
            htmlEntities.put( 246, "&ouml;");       // ï¿½ - lowercase o, umlaut
            htmlEntities.put( 248, "&oslash;");     // ï¿½ - lowercase o, slash
            htmlEntities.put( 249, "&ugrave;");     // ï¿½ - lowercase u, grave accent
            htmlEntities.put( 250, "&uacute;");     // ï¿½ - lowercase u, acute accent
            htmlEntities.put( 251, "&ucirc;");      // ï¿½ - lowercase u, circumflex accent
            htmlEntities.put( 252, "&uuml;");       // ï¿½ - lowercase u, umlaut
            htmlEntities.put( 253, "&yacute;");     // ï¿½ - lowercase y, acute accent
            htmlEntities.put( 254, "&thorn;");      // ï¿½ - lowercase thorn, Icelandic
            htmlEntities.put( 255, "&yuml;");       // ï¿½ - lowercase y, umlaut
            htmlEntities.put( 8364, "&euro;");      // Euro symbol
        }
    }

    public String transform(Object value) {
        String val = value.toString();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < val.length(); ++i) {
            int intVal = (int)val.charAt(i);
           if( htmlEntities.containsKey( intVal ) ) {
                builder.append( htmlEntities.get( intVal ) );
            } else if ( intVal > 128 ) {
                    builder.append( "&#" );
                    builder.append( intVal );
                    builder.append( ";" );
            } else {
                builder.append( val.charAt(i) );
            }
        }
        return builder.toString();
    }
}
